<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *群
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'群列表'),
	array('op'=>'edit','title'=>'添加群'),	
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>intval($_RQ['type']));
	$return['params']['type']=intval($_RQ['type']);
	$this->session('type',intval($_RQ['type']));
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('jy_quntui_user',$data);
		$params['mid']=$mid['mid'];
		$return['params']['mid']=$mid['mid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_quntui_qun',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['member']=$member;
			$item['user']=$user;
			if($this->session('type')==1){
				$item['url']=custom_url('jy_quntui/gochat//id='.$item['id'],true);
			}else{
				$item['url']=custom_url('jy_quntui/gochat/weixin/id='.$item['id'],true);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'群ID','type'=>'string'),
			array('name'=>'title','title'=>'群名称','type'=>'string'),
			array('type'=>'member','title'=>'发布人','name'=>'member'),
			array('name'=>'ewm','title'=>'群二维码','type'=>'image'),
			array('name'=>'ewmurl','title'=>'群链接','type'=>'copy'),
			array('name'=>'url','title'=>'加群链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'updatetime','title'=>'刷新时间','type'=>'time'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'id','width'=>15,'title'=>'群ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'发布人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'群标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_quntui_qun',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'));
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回群列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'群名称','type'=>'string'),
					array('name'=>'thumb','title'=>'群封面','type'=>'image','help'=>'留空会自动截取视频第一张图片/图集第一张'),
					array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
					array('name'=>'desc','title'=>'群介绍','type'=>'text'),
					array('type'=>'image','title'=>'群加入二维码','name'=>'ewm','help'=>'移步企业微信--客户联系--加客户--加入群聊--新建二维码--下载二维码'),
					array('type'=>'string','title'=>'群加入二维码链接地址','name'=>'ewmurl','help'=>'把上面的群加入二维码，通过https://jiema.wwei.cn/在线解码，把解码到的链接，填入此处！这样方便点击链接实现加入企业微信群！'),
					array('name'=>'usernum','title'=>'群人数','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写群名称！','error');
	}
	if(!$postdata['ewm']){
		exi('请上传群加入二维码！','error');
	}
	if(!$postdata['ewmurl']){
		exi('请填写群加入二维码链接地址！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'thumb'=>trim($postdata['thumb']),
		'type'=>intval($postdata['type']),
		'mid'=>intval($postdata['mid']),
		'desc'=>trim($postdata['desc']),
		'usernum'=>intval($postdata['usernum']),
		'ewm'=>trim($postdata['ewm']),
		'ewmurl'=>trim($postdata['ewmurl']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_quntui_qun',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_quntui_qun',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择群！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_quntui_qun',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_quntui_qun',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}